package com.lifeng.received;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * Created by lifeng on 2018/12/10.
 */
@Component
//监听消息队列
public class ReceivedService {
    //concurrency 队列并发数
    @RabbitListener(queues = "hello",concurrency = "10")
    public void onMessage(String hello) {
        System.out.println("hello:"+new Date().toString() + ",接收到消息:" + hello);
    }
    
    @RabbitListener(queues = "hello",containerFactory="simpleRabbitListenerContainerFactory")
    public void onMessage1(String hello) {
        System.out.println("hello:"+new Date().toString() + ",接收到消息:" + hello);
    }

    @RabbitListener(queues = "hello1")
    public void onMessage2(String hello) {
        System.out.println("hello1:"+new Date().toString() + ",接收到消息:" + hello);
    }
    @RabbitListener(queues = "hello2")
    public void onMessage3(String hello) {
        System.out.println("hello2:"+new Date().toString() + ",接收到消息:" + hello);
    }

    @RabbitListener(queues = "hello3")
    public void onMessage4(String hello) {
        System.out.println("hello3:"+new Date().toString() + ",接收到消息:" + hello);
    }
    @RabbitListener(queues = "hello4")
    public void onMessage5(String hello) {
        System.out.println("hello4:"+new Date().toString() + ",接收到消息:" + hello);
    }

    @RabbitListener(queues = "hello5")
    public void onMessage6(String hello) {
        System.out.println("hello5:"+new Date().toString() + ",接收到消息:" + hello);
    }
    @RabbitListener(queues = "hello6")
    public void onMessage7(String hello) {
        System.out.println("hello6:"+new Date().toString() + ",接收到消息:" + hello);
    }

    @RabbitListener(queues = "helloobj")
    public void onMessage8(@Payload byte[] message) {
        try {
            //TODO：接收String
            String result=new String(message,"UTF-8");
            System.out.println("helloobj:"+new Date().toString() + ",接收到消息:" + result);
        }catch (Exception e){
            //TODO
        }
    }

    /**消息确认机制*/
    @RabbitListener(queues = "hello7")
    public void onMessage9(Message message,Channel channel) throws IOException {
        try {
            System.out.println("接受成功:"+ new String(message.getBody(),"UTF-8"));
            // 采用手动应答模式, 手动确认应答更为安全稳定
            ////告诉服务器收到这条消息 已经被我消费了 可以在队列删掉
            // 这样以后就不会再发了 否则消息服务器以为这条消息没处理掉 后续还会在发
            //消息的标识，false只确认当前一个消息收到，true确认所有consumer获得的消息
            //throw new Exception("处理异常");
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }catch (Exception e){
            //ack返回false，并重新回到队列，
            //消息会一直重新入队列，一不小心就会xxxxx一大堆消息不断重复
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,true);
            System.out.println("处理失败fail");
        }

    }

    /**接受消息并响应*/
    @RabbitListener(queues = "hello8")
    public String onMessage10(String msg){
        System.out.println("hello8:"+new Date().toString() + ",接收到消息:" + msg);
        return "consumer:"+msg;
    }

    @RabbitListener(queues = "hello9")
    public void onMessage11(@Payload String body,@Headers Map<String,Object> headers){

    }

    /**接受消息并响应*/
    @RabbitListener(queues = "hello10")
    public void onMessage12(@Payload String body,@Header String token){

    }
}
